package handlers

import (
	"github.com/gin-gonic/gin"
	"real-reg-sys/models"
	"strconv"
)

func RealAdd(c *gin.Context) {
	username := c.PostForm("username")
	phone := c.PostForm("phone")
	real_room := c.PostForm("real_room")
	real_pay, _ := strconv.Atoi(c.PostForm("real_pay"))
	if username == "" || phone == "" || real_room == "" || real_pay == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Real{
		Username: username,
		Phone:    phone,
		RealRoom: real_room,
		RealPay:  real_pay,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "挂号失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "挂号成功",
	})
	return
}

func RealList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	offset := (page - 1) * size
	var reals []models.Real
	models.DB.Offset(offset).Limit(size).Find(&reals)
	var total int64
	models.DB.Model(&models.Real{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  reals,
		"total": total,
	})
	return
}
